package org.msfj.common.api;

import cn.hutool.core.util.StrUtil;
import org.msfj.common.feign.SubmailApi;
import org.msfj.common.config.SmsTemplateKeyConfig;
import org.msfj.common.model.CommonResult;
import org.msfj.common.submail.SubMailMessage;
import org.msfj.common.submail.SubMailMessages;
import org.msfj.common.submail.SubMailTemplate;
import org.msfj.common.util.JsonObjectUtils;
import org.msfj.common.util.SubMailUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description: 系统所有的短信发送接口实现
 * @Author: like
 * @Date: 2017-12-13 9:24
 * @Version: 1.0
 * @Created in idea by autoCode
 */
@RestController
public class SubmailApiImpl implements SubmailApi {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    SmsTemplateKeyConfig smsTemplateKeyConfig;

    @Autowired
    SubMailUtil subMailUtil;

    @Override
    public CommonResult<Boolean> sendSingle(String projectId, @RequestBody SubMailMessage subMailMessage) {
        return CommonResult.ok(subMailUtil.sendSubMailMessage(projectId, subMailMessage));
    }

    @Override
    public CommonResult<Boolean> sendMutil(String projectId, @RequestBody SubMailMessages subMailMessageList) {
        return CommonResult.ok(subMailUtil.sendSubMailMessage(projectId, subMailMessageList));
    }

    @Override
    public CommonResult<List<SubMailTemplate>> getTemplateList() {
        return CommonResult.ok(subMailUtil.getTemplateList());
    }

    @Override
    public CommonResult<SubMailTemplate> insertTemplate(@RequestBody SubMailTemplate subMailTemplateVo) {
        return CommonResult.ok(subMailUtil.insertTemplate(subMailTemplateVo));
    }

    @Override
    public CommonResult<Boolean> updateTemplate( @RequestBody SubMailTemplate subMailTemplateVo) {
        return CommonResult.ok(subMailUtil.updateTemplate(subMailTemplateVo));
    }

    @Override
    public CommonResult<Boolean> deleteTemplate( @RequestBody SubMailTemplate subMailTemplateVo) {
        return CommonResult.ok(subMailUtil.deleteTemplate(subMailTemplateVo));
    }

    @Override
    public CommonResult<Boolean> sendCommonPhoneValid( @RequestBody SubMailMessage subMailMessage) {
        String commonPhoneValidCode = smsTemplateKeyConfig.getCommonPhoneValidCode();
        if (StrUtil.isBlank(commonPhoneValidCode)) {
            logger.error("因为没有配置验证码短信模板，所以验证码无法发送成功，" + JsonObjectUtils.objectToJson(subMailMessage));
            return CommonResult.ok(true);
        }
        return CommonResult.ok(subMailUtil.sendSubMailMessage(commonPhoneValidCode, subMailMessage));
    }
}
